#include "publicstruct.h"
#include<QNetworkAccessManager>
#include<QNetworkRequest>
#include<QNetworkReply>
#include<QUrl>
#include<QFile>
#include<QEventLoop>
#include <QPainterPath>
#include <QPainter>
#include <QThread>
#include "mytcpsocket.h"



QPixmap downLoadFromUrl(const QString &url)
{
    QNetworkAccessManager manager;  //网络请求管理对象,控制应用程序发送请求和接收回复
    QNetworkRequest request;    //网络请求
    request.setUrl(QUrl(url));  //设置rul
    QNetworkReply *reply = manager.get(request);    //发送网络请求,服务器回复应答,触发finished()信号,此时quit()退出事件循环
    QEventLoop loop;    //事件循环
    QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
    loop.exec();    //开始事件循环

    // 处理错误情况
    if (reply->error() != QNetworkReply::NoError) {
        qWarning() << "Failed to download image:" << reply->errorString();
        return QPixmap();
    }
    //qDebug()<<"头像请求完成";

    // 读取图像数据并转换为QPixmap对象
    QByteArray data = reply->readAll();
    QPixmap pixmap;
    pixmap.loadFromData(data);  //以二进制流的形式初始化图片 (速度快)
    delete reply;   //释放QNetworkReply对象(网络应答)
    return pixmap;

}


QPixmap tailorCirclePixmap(const QPixmap t_pixmap, QLabel *label)
{
    QPixmap pixmap(t_pixmap);
    pixmap = pixmap.scaled(label->size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);

    int width = label->size().width();
    int height = label->size().height();

    QPixmap image(width,height);
    image.fill(Qt::transparent);

    QPainterPath painterPath;
    painterPath.addEllipse(0, 0, width, height);

    QPainter painter;
    painter.begin(&image);
    painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
    painter.setClipPath(painterPath);
    painter.drawPixmap(0, 0, width, height, pixmap);
    //绘制到label
    label->setPixmap(image);
    painter.end();

    return image;

}

QPixmap PixmapToRound(const QPixmap &src, int radius)
{
    // 图像为空,直接返回一个空的QPixmap
    if (src.isNull())
    {
        return QPixmap();
    }

    // 按比例缩放
    QPixmap t_pixmap;
    if(src.width() != radius || src.height() != radius)
    {
        t_pixmap = src.scaled(radius, radius, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
    }
    else
    {
        t_pixmap = src;
    }

    // 创建一个圆形的QPixmap
    QPixmap pixmap(radius,radius);
    // 设置背景透明
    pixmap.fill(Qt::transparent);

    QPainter painter(&pixmap);
    // 抗锯齿,平滑图像转换
    painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);

    // 创建一个圆形裁剪路径
    QPainterPath path;
    path.addEllipse(0, 0, radius, radius);
    // 设置裁剪路径,意味着接下来的绘制操作只会在圆形区域内进行
    painter.setClipPath(path);
    // 将先前缩放或未缩放的pixmapa绘制到pixmap上,但由于设置了裁剪路径,所以只有圆形区域内的内容会被绘制。
    painter.drawPixmap(0, 0, radius, radius, t_pixmap);
    // 返回绘制好的圆形QPixmap
    return pixmap;
}

void downLoadSaveFromUrl(const QString &url, const QString local)
{
    QPixmap pixmap = downLoadFromUrl(url);
    pixmap.save(local,"PNG");
}

void setQssStyle(const QString &qss,QWidget* widget)
{
    QFile file(qss);
    if(!file.open(QIODevice::ReadOnly))
        qDebug()<<QString("%1 not open").arg(file.fileName());

    QString style = file.readAll();
    widget->setStyleSheet(style);
}

// 这个函数假设输入的QPixmap使用的是ARGB32格式
QPixmap PixmapToRoundOpenCV(const QPixmap &pixmap)
{

    // 将QPixmap转换为QImage
    QImage originalImage = pixmap.toImage();

    // 将QImage转换为OpenCV的Mat对象
    cv::Mat originalMat(originalImage.height(), originalImage.width(), CV_8UC4, (void*)originalImage.bits(), originalImage.bytesPerLine());

    // 创建与原始图像大小相同的Mat对象，用于存储处理后的图像
    cv::Mat circularMat = cv::Mat::zeros(originalMat.size(), originalMat.type());

    // 创建圆形遮罩
    int radius = cv::min(originalMat.cols, originalMat.rows) / 2;
    cv::Point center(originalMat.cols / 2, originalMat.rows / 2);
    cv::circle(circularMat, center, radius, cv::Scalar(255, 255, 255, 255), cv::FILLED);

    // 将原始图像与圆形遮罩结合，保留圆形区域内的像素，其他区域设置为透明
    originalMat.copyTo(circularMat, circularMat > 0);

    // 将处理后的Mat对象转回QImage
    QImage circularImage((const unsigned char*)(circularMat.data), circularMat.cols, circularMat.rows, circularMat.step, QImage::Format_ARGB32);

    // 将QImage转换为QPixmap并返回
    return QPixmap::fromImage(circularImage);
}

QPixmap RoundBorderPixmap(const QPixmap &originalPixmap, int borderWidth, const Scalar &borderColor)
{

    // 将QPixmap转换为QImage
    QImage originalImage = originalPixmap.toImage();

    // 将QImage转换为OpenCV的Mat对象
    cv::Mat originalMat(originalImage.height(), originalImage.width(), CV_8UC4, (void*)originalImage.bits(), originalImage.bytesPerLine());

    // 创建与原始图像大小相同的Mat对象，用于存储处理后的图像
    cv::Mat circularMat = cv::Mat::zeros(originalMat.size(), originalMat.type());

    // 创建圆形遮罩
    int radius = cv::min(originalMat.cols, originalMat.rows) / 2;
    cv::Point center(originalMat.cols / 2, originalMat.rows / 2);
    cv::circle(circularMat, center, radius, cv::Scalar(255, 255, 255, 255), cv::FILLED);

    // 将原始图像与圆形遮罩结合，保留圆形区域内的像素，其他区域设置为透明
    originalMat.copyTo(circularMat, circularMat > 0);

    // 在圆形图像周围添加边框
    int borderRadius = radius - borderWidth;
    cv::circle(circularMat, center, borderRadius, borderColor, borderWidth);

    // 将处理后的Mat对象转回QImage
    QImage circularImage((const unsigned char*)(circularMat.data), circularMat.cols, circularMat.rows, circularMat.step, QImage::Format_ARGB32);

    // 将QImage转换为QPixmap并返回
    return QPixmap::fromImage(circularImage);

}
